/**
 * Copyright (C) 2013 DaiKit.com - daikit4gxt-sample module (admin@daikit.com)
 *
 *         Project home : http://code.daikit.com/commons
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.daikit.daikit4gxtsample.client.screen.testscreen.producteditor;

import com.daikit.daikit4gxt.client.action.DkBaseAction;
import com.daikit.daikit4gxt.client.ui.forms.DkFixedFormPanel;
import com.daikit.daikit4gxtsample.client.Main;
import com.daikit.daikit4gxtsample.client.ui.field.CountryDetailReferenceEditorGrid;
import com.daikit.daikit4gxtsample.client.ui.field.ShopDualGrid;
import com.daikit.daikit4gxtsample.client.ui.field.SupplierDetailEditorGrid;
import com.daikit.daikit4gxtsample.shared.bean.CountryDetailData;
import com.daikit.daikit4gxtsample.shared.bean.SupplierDetailData;
import com.sencha.gxt.widget.core.client.TabItemConfig;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.event.SelectEvent.SelectHandler;
import com.sencha.gxt.widget.core.client.form.FieldLabel;


/**
 * Product Editor Tab 1
 * 
 * @author tcaselli
 * @version $Revision$ Last modifier: $Author$ Last commit: $Date$
 */
public class ProductEditorTab1 extends AbstractEditorTab
{

	private final DkFixedFormPanel myFormPanel;

	private final FieldLabel productManagerFieldLabel;
	private final ShopDualGrid shopDualGrid;

	/**
	 * Tab 1 of the editor
	 * 
	 * @param controller
	 */
	public ProductEditorTab1(final ProductEditorController controller)
	{
		super(controller);
		final TabItemConfig tabItemConfig = new TabItemConfig();
		tabItemConfig.setIcon(Main.icons().table_16());
		tabItemConfig.setText(Main.gwtSampleI18n().test_screen_editor_tab_1_title());
		setTabItemConfig(tabItemConfig);

		final FieldLabel idFieldLabel = new FieldLabel(getEditor().id, Main.gwtSampleI18n().product_id());
		final FieldLabel nameFieldLabel = new FieldLabel(getEditor().name, Main.gwtSampleI18n().product_name());
		final FieldLabel detailFieldLabel = new FieldLabel(getEditor().productDetailEditor.detail, Main.gwtSampleI18n()
				.product_detail_detail());
		final FieldLabel propertyDoubleFieldLabel = new FieldLabel(getEditor().propertyDouble, Main.gwtSampleI18n()
				.product_propertyDouble());
		final FieldLabel propertyRichTextFieldLabel = new FieldLabel(getEditor().propertyRichText, Main.gwtSampleI18n()
				.product_propertyRichText());
		productManagerFieldLabel = new FieldLabel(getEditor().productManager, Main.gwtSampleI18n().product_manager());

		final SupplierDetailEditorGrid supplierDetailsGrid = new SupplierDetailEditorGrid(getEditor().supplierDetails.getStore())
		{
			@Override
			public void invalidateUi()
			{
				setEnabled(Main.model().isLogged() && getEditor().isEditing());
			}

			@Override
			protected boolean isModelDeletable(final SupplierDetailData model, final int column, final int row, final String modelKey)
			{
				// TODO Auto-generated method stub
				final boolean deletable = super.isModelDeletable(model, column, row, modelKey);
				return deletable;
			}
		};

		supplierDetailsGrid.getToolbar().add(new TextButton("debug", new SelectHandler()
		{
			@Override
			public void onSelect(final SelectEvent event)
			{
				supplierDetailsGrid.setEnabled(!supplierDetailsGrid.isEnabled());
			}
		}));

		final FieldLabel supplierDetailsFieldLabel = new FieldLabel(supplierDetailsGrid, Main.gwtSampleI18n()
				.product_supplier_details());

		final FieldLabel localizedPropertyFieldLabel = new FieldLabel(getEditor().localizedProperty, Main.gwtSampleI18n()
				.product_localized_property());

		final CountryDetailReferenceEditorGrid countryDetailsEditor = new CountryDetailReferenceEditorGrid(
				getEditor().countryDetails.getStore())
		{
			@Override
			public void invalidateUi()
			{
				setEnabled(Main.model().isLogged() && getEditor().isEditing());
			}

			@Override
			protected boolean isModelDeletable(final CountryDetailData model, final int column, final int row, final String modelKey)
			{
				return true;
			}
		};
		final FieldLabel countryDetailsFieldLabel = new FieldLabel(countryDetailsEditor, Main.gwtSampleI18n()
				.product_country_details());

		final FieldLabel commentsFieldLabel = new FieldLabel(getEditor().comments, Main.gwtSampleI18n().product_comments());

		shopDualGrid = new ShopDualGrid(getEditor().shops.getStore())
		{
			@Override
			public void invalidateUi()
			{
				setEnabled(Main.model().isLogged() && getEditor().isEditing());
			}
		};
		final FieldLabel shopsFieldLabel = new FieldLabel(shopDualGrid, Main.gwtSampleI18n().product_shops());

		myFormPanel = new DkFixedFormPanel(2);

		myFormPanel.add(idFieldLabel, 0);
		myFormPanel.add(nameFieldLabel, 1);
		myFormPanel.add(detailFieldLabel, 0);
		myFormPanel.add(propertyDoubleFieldLabel, 1);
		myFormPanel.add(propertyRichTextFieldLabel);
		myFormPanel.add(supplierDetailsFieldLabel);
		myFormPanel.add(shopsFieldLabel);
		myFormPanel.add(productManagerFieldLabel, 0);
		myFormPanel.add(localizedPropertyFieldLabel, 1);
		myFormPanel.addNewRow();
		myFormPanel.add(commentsFieldLabel, 0);
		myFormPanel.add(countryDetailsFieldLabel, 1);

		add(myFormPanel);
	}

	@Override
	public void invalidateUi()
	{
		getTabItemConfig().setEnabled(Main.model().isLogged() && getEditor().isEditing());
		getEditor().id.setEnabled(Main.model().isLogged() && getEditor().isEditing());
		getEditor().propertyRichText.setEnabled(Main.model().isLogged() && getEditor().isEditing());
		getEditor().name.setEnabled(Main.model().isLogged() && getEditor().isEditing());
		getEditor().productDetailEditor.detail.setEnabled(Main.model().isLogged() && getEditor().isEditing());
		getEditor().propertyDouble.setEnabled(Main.model().isLogged() && getEditor().isEditing());
		getEditor().localizedProperty.setEnabled(Main.model().isLogged() && getEditor().isEditing());
		productManagerFieldLabel.setEnabled(false);
	}

	@Override
	public boolean needToBeReloaded()
	{
		return true;
	}

	@Override
	public DkBaseAction<?> getReloadSubPanelAction(final boolean force, final Object... optionalArgs)
	{
		return shopDualGrid.getReloadLeftAction();
	}
}
